package com.ming.transaction.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ming.transaction.common.Result;
import com.ming.transaction.common.annotation.RecordLoggerAnnotation;
import com.ming.transaction.entity.Dto.LoAndRiDto;
import com.ming.transaction.entity.Dto.PassWordDto;
import com.ming.transaction.entity.User;
import com.ming.transaction.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 陈仕健
 * @since 2023-04-27
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户控制器")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 用户登录 And 注册接口
     * @param loAndRiDto
     * @return
     */
    @ApiOperation("登录注册控制器")
    @PostMapping("/loginRig")
    public Result LoginRig(@RequestBody LoAndRiDto loAndRiDto){
        return userService.loginRig(loAndRiDto);
    }

    /**
     * 根据前端发送的数据进行分页查询
     * @param pageNum
     * @param pageSize
     * @param nickname
     * @param number
     * @param phone
     * @param address
     * @return
     */
    @ApiOperation("用户分页查询接口")
    @GetMapping("/page")
    public Result PageSearch(@RequestParam Integer pageNum,
                             @RequestParam Integer pageSize,
                             @RequestParam(defaultValue = "") String nickname,
                             @RequestParam(defaultValue = "") String number,
                             @RequestParam(defaultValue = "") String phone,
                             @RequestParam(defaultValue = "") String address){
        return userService.pageSeach(pageNum,pageSize,nickname,number,phone,address);
    }

    /**
     * 管理员手动添加用户
     * @param user
     * @return
     */
    @PostMapping
    @ApiOperation("管理员添加用户")
    @RecordLoggerAnnotation(value = "管理员添加用户")
    public Result save(@RequestBody User user){
        return userService.addUser(user);
    }

    /**
     * 管理员修改用户信息
     * @param user
     * @return
     */
    @PutMapping
    @ApiOperation("管理员修改用户")
    @RecordLoggerAnnotation(value = "管理员修改用户")
    public Result editUser(@RequestBody User user){
        return userService.editUser(user);
    }

    /**
     * 管理员手动删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation("管理员删除用户")
    @RecordLoggerAnnotation(value = "管理员删除用户")
    public Result delete(@PathVariable Integer id){
        return userService.deleteById(id);
    }


    /**
     * 查询所有用户
     * @return
     */
    @GetMapping
    @ApiOperation("查询所有用户")
    public Result findAll(){
        return Result.success("查询所有用户信息!",userService.list());
    }


    /**
     * 批量删除用户
     * @param ids
     * @return
     */
    @PostMapping("/del/batch")
    @ApiOperation("管理员批量删除用户")
    @RecordLoggerAnnotation(value = "管理员批量删除用户")
    public Result deletebatch(@RequestBody List<Integer> ids) {
        return userService.deleteByIds(ids);
    }

    /**
     * 用户头像上传接口
     * @param file
     * @return
     * @throws IOException
     */
    @ApiOperation("用户头像上传")
    @PostMapping("/upload")
    public Result upload(@RequestParam MultipartFile file) throws IOException {
        return userService.uploadImage(file);
    }

    /**
     * 用户头像加载接口
     * @param fileUUID
     * @throws IOException
     */
    @ApiOperation("用户头像获取")
    @GetMapping(value = "/load/{fileUUID}",produces = "image/jpeg")
    public void download(@PathVariable String fileUUID) throws IOException{
        userService.dowmloadImage(fileUUID);
    }

    /**
     * 根据用户账号查询用户信息
     * @param number
     * @return
     */
    @ApiOperation("根据用户账号查询用户信息")
    @GetMapping("/{number}")
    public Result SearchUserByNumber(@PathVariable("number") String number){
        return userService.UserByNumber(number);
    }


    /**
     * 用户修改密码
     * @param passWordDto
     * @return
     */
    @ApiOperation("用户修改密码")
    @PutMapping("/EditP")
    public Result EditPassWord(@RequestBody PassWordDto passWordDto){
        return  userService.editPassword(passWordDto);
    }
}

